<?php
class loginModel{
	private $db;
	private $ecs;
	public function __construct(){
		$this->db = $GLOBALS['db'];
		$this->ecs = $GLOBALS['ecs'];
	}
	public function is_appid()
    {
    	$sql="SELECT op_value FROM ".$this->ecs->table('options')." WHERE op_key='appid'";
    	$is_appid=$this->db->getOne($sql);
		
    	return  $is_appid;
    }
	public function is_ser()
    {
    	$sql="SELECT op_value FROM ".$this->ecs->table('options')." WHERE op_key='appsecret'";
    	$is_ser=$this->db->getOne($sql);
    	
    	return  $is_ser;
    }
    
    public function get_appid_ser(){
    	$sql ="SELECT * FROM `wxch_config` WHERE `id` = 1";
    	$res=$this->db->getRow($sql);
    	return $res;
    }
    public function is_user_existed($openid){
    	$sql="SELECT uid  FROM wxch_user WHERE wxid= '".$openid."'";
    	$res=$this->db->getOne($sql);
    	return $res;
    }
    public function insertUser($info){
    
    	$field='';
    	$value='';
    	$result=array('error'=>0,'content'=>'');
    
    	foreach($info as $k=>$v)
    	{
    		if($k!='privilege')
    		{
    			if($k=="openid"){
    				
    			}else{
    			$field.="$k,";
    			$value.="'".$v."',";
    			}
    
    
    				
    		}
    	}
    
    	$field=substr($field,0,strlen($field)-1);
    	$value=substr($value,0,strlen($value)-1);
    		
    	$sql="INSERT INTO wxch_user (".$field.") VALUES(".$value.")";
    	if(!$res=$this->db->query($sql))
    	{
    		$result['error']=1;
    		$result['content']='MySQL Query Error';
    	}
    
    	return $this->db->insert_id();
    }
    
    public function updateUser($info, $wxid) {
		foreach ( $info as $k => $v ) {
			$where .= " $k='$v',";
		}
		
		$where = substr ( $where, 0, strlen ( $where ) - 1 );
		
		$sql = "UPDATE wxch_user SET " . $where . " WHERE uid='" . $wxid . "'";
		
    	return $res=$this->db->query($sql);
	}
	public function getLoginInfo($wxid){
		//$wxid = $this->get_openid($wxid);
		$sql=" SELECT w.uid as id ,u.user_id as uid,u.user_name,w.headimgurl,u.user_money,u.pay_points,u.mobile_phone,u.nickname as uName,w.nickname as wName FROM ".$this->ecs->table ( 'users' )." as u, wxch_user as w WHERE u.wxid=w.wxid and w.uid = '".$wxid ."'";
		$res = $this->db->getRow($sql,true);
		$res['headimgurl'] = subHeadimgurl($res['headimgurl']);
		if($res){
			return $res;
		}else{
			return false;
		}
	}
	public function isUser($wxid){
		$sql="SELECT user_id FROM ".$this->ecs->table('users')." WHERE wxid= '".$wxid."'";
		$res=$this->db->getOne($sql);
		return $res;
	}
	public function insertECSUser($arr,$wxid){
		$field='';
		$value='';
		$result=array('error'=>0,'content'=>'');
		
		$field.='user_name,';
		$value.="'".$arr['nickname']."',";
		$field.="sex,";
		$value.=$arr['sex'].",";
		$field.="reg_time,";
		$value.=time().",";
		$field.="wxid";
		$value.="'".$wxid."'";
		
		$sql="INSERT INTO ".$this->ecs->table('users')." (".$field.") VALUES (".$value.")";
		if(!$res=$this->db->query($sql))
		{
			$result['error']=1;
			$result['content']='MySQL Query Error';
		}
		
		return $this->db->insert_id();
	}
	public function saveInfo($info,$wxid){
		$wxid = $this->get_openid($wxid);
		foreach ( $info as $k => $v ) {
			$where .= " $k='$v',";
		}
		$where = substr ( $where, 0, strlen ( $where ) - 1 );
		$sql = "UPDATE " . $this->ecs->table ( 'users' ) . " SET " . $where . " WHERE wxid='" . $wxid . "'";
    	return $res=$this->db->query($sql);
	}
	
	public function get_openid($wxid){
		$sql = "SELECT wxid  FROM wxch_user WHERE uid = '".$wxid."' LIMIT 1";
		return $this->db->getOne($sql);
	}
	
	public function get_login_info($wxid)
	{
		//$wxid = $this->get_openid($wxid);
		$sql=" SELECT w.uid as id ,u.user_id as uid,u.user_name,w.headimgurl,u.user_money,u.pay_points,u.mobile_phone,u.nickname as uName,w.nickname as wName,u.notify FROM ".$this->ecs->table ( 'users' )." as u, wxch_user as w WHERE u.wxid=w.wxid and w.uid = '".$wxid ."'";
		$user = $this->db->getRow($sql,true);
		
		
		if($user){
		    $_SESSION['user_id'] = $user['uid'];
		    $_SESSION['notify'] = $user['notify'];
// 		    $user['notify'] = "$sql";
			$user['headimgurl'] = subHeadimgurl($user['headimgurl']);
			$res['user'] = $user;
			
			$sql = "SELECT id,content,create_time FROM ".$this->ecs->table('news')." WHERE create_id = $wxid ORDER BY create_time DESC LIMIT 5 ";
			$suipai =  $this->db->getAll($sql);
			foreach ($suipai as $k=>$v)
			{
				$suipai[$k]['day'] = date("d",$v['create_time']);
				$suipai[$k]['month'] = date("m",$v['create_time']);
				//缩略图
				$sql = "SELECT thumb FROM ".$this->ecs->table('news_img')." WHERE news_id = ".$v['id']." LIMIT 1 ";
				$suipai[$k]['thumb'] = $this->db->getOne($sql);
			}
			$res['suipai'] = $suipai;
			
			
			$sql = "SELECT p1.news_id,p1.commenter,p1.comment_time,p1.comment,p1.parent_id,p2.headimgurl,p2.nickname as name ".
					" FROM ".$this->ecs->table('news_comment')." p1 ".
					" LEFT JOIN `wxch_user` p2  ON p1.commenter=p2.uid ".
					" WHERE p1.commenter = $wxid AND p1.is_delete = 0 ORDER BY p1.comment_time DESC LIMIT 5 ";
			$comment = $this->db->getAll($sql);
			$comm = array();
			foreach ($comment as $k=>$v)
			{
				$comment[$k]['headimgurl'] = subHeadimgurl($v['headimgurl']);
				//缩略图
				$sql = "SELECT thumb FROM ".$this->ecs->table('news_img')." WHERE news_id = ".$v['news_id']." LIMIT 1 ";
				$comment[$k]['thumb'] = $this->db->getOne($sql);
// 				switch ($v['parent_id'])
// 				{
// 					case 0 :
// 						$comm[] = $comment[$k];
// 						break;
// 					default:
// 						$comm[] = $this->get_comment_child($v['parent_id'],$comment[$k]['thumb']);
// 				}
				if($v['parent_id']!=0)
				{
					$sql = "SELECT p1.comment,p2.nickname as name,p1.is_delete ".
							" FROM ".$this->ecs->table("news_comment")." p1 ".
							" LEFT JOIN `wxch_user` p2  ON p1.commenter=p2.uid ".
							" WHERE p1.id = ".$v['parent_id']." LIMIT 1 ";
					$comment[$k]['parentComment'] = $this->db->getRow($sql);
					if($comment[$k]['parentComment']['is_delete']==1)//已删除
					{
						$comment[$k]['parentComment']['name'] = '';
						$comment[$k]['parentComment']['comment'] = '用户已删除原评论';
				
					}
				}else{
					$comment[$k]['parentComment'] = array();
				}
				$comm[] = $comment[$k];
			}
			$res['comment'] = $comm;
			return $res;
		}else{
			return false;
		}
	}
	
	public function get_comment_child($parent_id,$thumb){
		$sql = "SELECT p1.id,p1.news_id,p1.commenter,p1.comment_time,p1.comment,p1.parent_id,p2.headimgurl,p2.nickname as name ".
					" FROM ".$this->ecs->table('news_comment')." p1 ".
					" LEFT JOIN `wxch_user` p2  ON p1.commenter=p2.uid ".
					" WHERE p1.id = $parent_id LIMIT 1 ";
		$comment = $this->db->getRow($sql);
		$comment['headimgurl'] = subHeadimgurl($comment['headimgurl']);
		$comment['thumb'] = $thumb;
		$sql = "SELECT p1.comment,p2.nickname as name ".
					" FROM ".$this->ecs->table('news_comment')." p1 ".
					" LEFT JOIN `wxch_user` p2  ON p1.commenter=p2.uid ".
					" WHERE p1.parent_id = $parent_id ORDER BY p1.comment_time ";
		$child = $this->db->getAll($sql);
		$comment['child'] = $child;
		return $comment;
	}
}